Vue.component('v-select', VueSelect.VueSelect);
let postQuestionApp = new Vue({
    el: '#postQuestionApp',
    data: {
        title : null,
        tags: [
            // { label: 'Spring', value: 1 },
            // { label: 'SpringMvc', value: 2 },
            // { label: 'Mybatis', value: 3 },
            // { label: 'MybatisPlus', value: 4 },
            // { label: 'SpringBoot', value: 5 }
        ],
        selectedTagIds: [],
        teachers : [
            // { label: '小王', value: 1 },
            // { label: '小范', value: 2 },
            // { label: '小刘', value: 3 },
            // { label: '小成', value: 4 },
            // { label: '小程', value: 5 },
            // { label: '大刘', value: 6 }
        ],
        selectedTeacherIds: []
    },
    methods: {
        loadTags: function () {
            $.ajax({
                url: '/portal/tags',
                success: function (r) {
                    let list = r.data;
                    let tags = [];
                    for (let i = 0; i < list.length; i++) {
                        let op = {};
                        op.label = list[i].name
                        op.value = list[i].id
                        tags[i] = op
                    }
                    postQuestionApp.tags = tags;
                }
            });
        },
        loadTeachers: function () {
            $.ajax({
                url: '/portal/user/teachers',
                success: function (r) {
                    let list = r.data;
                    let teachers = [];
                    for (let i = 0; i < list.length; i++) {
                        let op = {};
                        op.label = list[i].nickname;
                        op.value = list[i].id;
                        teachers[i] = op
                        console.log(op)
                    }
                    postQuestionApp.teachers = teachers
                }
            });
        },
        postQuestion:function () {
            let data={
                'title': postQuestionApp.title,
                'content':$('#summernote').val(),
                'tagIds':postQuestionApp.selectedTagIds,
                'teacherIds':postQuestionApp.selectedTeacherIds
            }
            console.log(data)
            console.log('即将提交服务器端的请求参数')
            $.ajax({
                url:'/portal/questions/post',
                data:data,
                type:'post',
                traditional:true,
                success:function (r) {
                    if(r.state==2000){
                        alert('发布问题成功')
                    }else {
                        alert(r.message)
                    }
                }
            })
        }
    },
    created:function () {
        this.loadTags()
        this.loadTeachers()
        this.postQuestion()
    }
});

